package com.submarinusoftware.periscoffer.view.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.submarinusoftware.periscoffer.R;
import com.submarinusoftware.periscoffer.entities.Offer;
import com.submarinusoftware.periscoffer.helpers.ImageLoader;

public class OfferListViewAdapter extends ArrayAdapter<Offer> {

    private ArrayList<Offer> theOffers;
    public ImageLoader theImageLoader; 
    private static LayoutInflater theInflater=null;    


    public OfferListViewAdapter(Context context, int textViewResourceId, ArrayList<Offer> items) {
            super(context, textViewResourceId, items);
            this.theOffers = items;
            theInflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);            
            theImageLoader=new ImageLoader(getContext().getApplicationContext());
    }
    
    public static class ViewHolder{
        public TextView title;
        public TextView description;
        public ImageView image;
    }    

    public View getView(int position, View convertView, ViewGroup parent) {
            View vi=convertView;
            ViewHolder holder;            
            
            if (vi == null) {
                vi = theInflater.inflate(R.layout.offer_row, null);
                holder=new ViewHolder();
                holder.title=(TextView)vi.findViewById(R.id.toptext);
                holder.description=(TextView)vi.findViewById(R.id.bottomtext);;             
                holder.image=(ImageView)vi.findViewById(R.id.icon);
                vi.setTag(holder);            	
            	
                //LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                //v = vi.inflate(R.layout.offer_row, null);
            }
            else holder=(ViewHolder)vi.getTag();       
           
            Offer o = theOffers.get(position);

            if (o != null) {
                TextView  tt = (TextView) vi.findViewById(R.id.toptext);
                TextView  bt = (TextView) vi.findViewById(R.id.bottomtext);

                if (tt != null) 
                    tt.setText(o.getName()); 

                if(bt != null)
                    bt.setText(o.getDescription());

                holder.image.setTag(this.theOffers.get(position).getImageURI());
                theImageLoader.DisplayImage(this.theOffers.get(position).getImageURI(), (Activity) getContext(), holder.image);
            
            }
            return vi;            
            
/*            Offer o = items.get(position);
            if (o != null) {
            		ImageView im = (ImageView) v.findViewById(R.id.icon);
                    TextView  tt = (TextView) v.findViewById(R.id.toptext);
                    TextView  bt = (TextView) v.findViewById(R.id.bottomtext);
                
                    if (tt != null) 
                          tt.setText(o.getName()); 
                    
                    if(bt != null)
                          bt.setText(o.getDescription());

                    if (im != null)
                        theImageLoader.DisplayImage(o.getImageURI(), (Activity) getContext(), im);
                    
            }
            return v;*/
    }
}